<template>
  <div class="messageManage h100" v-loading="loading">
    <div class="pageContent">
      <el-row :gutter="11" class="fileSearch">
        <el-form
          :model="ruleForm"
          :inline="true"
          ref="ruleForm"
          label-width="80px"
        >
          <!-- <el-form-item label="手机号:" prop="loginPhone">
            <el-input v-model="ruleForm.loginPhone" clearable></el-input>
          </el-form-item>
          <el-form-item label="姓名:" prop="fullName">
            <el-input v-model="ruleForm.fullName" clearable></el-input>
          </el-form-item>
          <el-form-item> -->
          <!-- <el-button
              icon="el-icon-search"
              type="primary"
              @click="onSubmit('search')"
            >
              查询
            </el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item> -->
          <el-button
            icon="el-icon-plus"
            type="success"
            @click="dialogVisible = true"
            >新增</el-button
          >
        </el-form>
      </el-row>
     <div class="table_wrapper">
        <el-table
        :data="tableData"
        border
        :header-cell-style="{
          background: 'rgb(246,247,251)',
          color: '#606266'
        }"
        stripe
        class="defaultTableHeight"
      >
        <el-table-column type="index" label="编号" width="50">
        </el-table-column>
        <!-- <el-table-column prop="gmtCreate" align="center" label="注册日期">
        </el-table-column> -->
        <el-table-column prop="title" align="center" label="标题">
        </el-table-column>
        <el-table-column prop="content" align="center" label="内容">
        </el-table-column>
        <!-- <el-table-column prop="asLogin" align="center" label="禁用启用">
          <template slot-scope="scope">
            <el-button
              :type="scope.row.asLogin === 0 ? 'danger' : 'success'"
              size="mini"
              >{{ scope.row.asLogin === 0 ? "禁用" : "启用" }}
            </el-button>
          </template>
        </el-table-column> -->
        <el-table-column label="操作" align="center" width="300px">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="openDielog(scope.row)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" @click="del(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <p style="text-align: right;">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pager.currentPage"
          :page-size="pager.pageSize"
          :page-sizes="[10, 20, 30, 50]"
          background
          layout="total,sizes,prev, pager, next, jumper"
          :total="pager.total"
        >
        </el-pagination>
      </p>
     </div>
    </div>
    <el-dialog
      title="添加商品类型"
      :visible.sync="dialogVisible"
      width="800px"
      v-dialogDrag
      @close="closeDialog"
    >
      <el-form
        ref="goodsForm"
        :model="goodsForm"
        :rules="goodsFormRules"
        label-width="80px"
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="goodsForm.title"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input type="textarea" v-model="goodsForm.content"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveGoods">保存</el-button>
          <el-button @click="closeDialog">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  message_getmessagelist,
  message_save,
  message_delete
} from "@/api/messageManage";
export default {
  name: "messageManage",
  data() {
    return {
      loading: false,
      dialogVisible: false,
      ruleForm: {
        fullName: ""
      },
      pager: {
        pageSize: 20,
        total: 1,
        currentPage: 1
      },
      tableData: [],
      goodsForm: {},
      goodsFormRules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        content: [{ required: true, message: "请输入内容", trigger: "blur" }]
      }
    };
  },
  mounted() {
    this.onSubmit();
  },
  //注销window.onresize事件
  destroyed() {},
  methods: {
    async onSubmit() {
      let postData = { messageType: 1 };
      console.log(postData, "postData");
      this.loading = true;
      try {
        const res = await message_getmessagelist(postData);
        this.loading = false;
        console.log(res, 200);
        if (res.code === "2000") {
          this.tableData = res.data;
          return;
        }
        this.$message.fail(res.msg || "数据请求失败");
      } catch (err) {
        console.log(err, 404);
        this.$message.fail(err.msg);
      }
    },
    openDielog(data) {
      let { title, content, id, messageType } = data;
      this.goodsForm = {
        title,
        content,
        id,
        messageType
      };
      this.dialogVisible = true;
    },
    async saveGoods() {
      let { title, content, id } = this.goodsForm;
      let postData = {
        messageType: 1,
        title,
        content,
        id
      };
      console.log(postData, "postData");

      try {
        const res = await message_save(postData);
        console.log(res, 200);
        if (res.code === "2000") {
          this.$message.success("保存成功");
          this.closeDialog();
          this.onSubmit();
          return;
        }
        this.$message.fail(res.msg || "数据获取失败");
      } catch (err) {
        console.log(err, 404);
        this.$message.fail(err.msg);
      }
    },
    async del({ id }) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          let postData = { ids: [id] };
          console.log(postData, "postData");
          try {
            const res = await message_delete(postData);
            console.log(res, 200);
            if (res.code === "2000") {
              this.$message.success("删除成功");
              this.onSubmit();
              return;
            }
            this.$message.fail(res.msg || "数据请求失败");
          } catch (err) {
            console.log(err, 404);
            this.$message.fail(err.msg);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 关闭弹窗的回调
    closeDialog() {
      this.goodsForm = { id: "", title: "", content: "", messageType: 1 };
      this.$refs["goodsForm"].resetFields();
      this.dialogVisible = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs["ruleForm"].resetFields();
      this.onSubmit();
    },
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function(size) {
      this.pager.pageSize = size;
      this.onSubmit();
    },
    handleCurrentChange: function(currentPage) {
      this.pager.currentPage = currentPage;
      this.onSubmit(); //点击第几页
    }
  }
};
</script>

<style lang="scss" scoped>
.el-table {
  margin-top: 20px;
}
</style>
